<template>
  <div>
    <img :src="violationImage" alt="违规图片">
    <button type="primary" @click="getViolationImage_">请求</button>
  </div>
</template>

<script>
import {getViolationImage} from "../../../../api/data";

export default {
  name: "violate-component3",
  data() {
    return {
      violationImage: ''
    }
  },
  methods: {
    // getViolationImage_(){
    //   getViolationImage().then(res=>{
    //     console.log(66, res.data)
    //     const blob = new Blob([res.data], { type: 'image/jpeg' })
    //     const u = window.URL.createObjectURL(blob)
    //     console.log(u)
    //     this.violationImage = u
    //   }).catch(err=>{
    //
    //   })
    // },
    getViolationImage_() {
      getViolationImage().then(res => {
        console.log(res)
        console.log(res.data.obj)
        this.violationImage = 'data:image/jpeg;base64,' + res.data.obj
        console.log(this.violationImage)
      })
    },
    arrayBufferToBase64(buffer) {
      var binary = ''
      var bytes = new Uint8Array(buffer)
      var len = bytes.byteLength
      for (var i = 0; i < len; i++) {
        binary += String.fromCharCode(bytes[i])
      }
      return window.btoa(binary)
    },
    blobToBase64(blob) {
      return new Promise((resolve, reject) => {
        const fileReader = new FileReader();
        fileReader.onload = (e) => {
          resolve(e.target.result);
        };
        //readAsDataURL
        fileReader.readAsDataURL(blob);
        fileReader.onerror = () => {
          reject(new Error("文件流异常"));
        }
      })
    },
    //字符串转字节序列
    stringToByte(str) {
      var bytes = new Array();
      var len, c;
      len = str.length;
      for (var i = 0; i < len; i++) {
        c = str.charCodeAt(i);
        if (c >= 0x010000 && c <= 0x10FFFF) {
          bytes.push(((c >> 18) & 0x07) | 0xF0);
          bytes.push(((c >> 12) & 0x3F) | 0x80);
          bytes.push(((c >> 6) & 0x3F) | 0x80);
          bytes.push((c & 0x3F) | 0x80);
        } else if (c >= 0x000800 && c <= 0x00FFFF) {
          bytes.push(((c >> 12) & 0x0F) | 0xE0);
          bytes.push(((c >> 6) & 0x3F) | 0x80);
          bytes.push((c & 0x3F) | 0x80);
        } else if (c >= 0x000080 && c <= 0x0007FF) {
          bytes.push(((c >> 6) & 0x1F) | 0xC0);
          bytes.push((c & 0x3F) | 0x80);
        } else {
          bytes.push(c & 0xFF);
        }
      }
      return bytes;
    },
//字节序列转ASCII码
//[0x24, 0x26, 0x28, 0x2A] ==> "$&C*"
    byteToString(arr) {
      if (typeof arr === 'string') {
        return arr;
      }
      var str = '',
        _arr = arr;
      for (var i = 0; i < _arr.length; i++) {
        var one = _arr[i].toString(2),
          v = one.match(/^1+?(?=0)/);
        if (v && one.length == 8) {
          var bytesLength = v[0].length;
          var store = _arr[i].toString(2).slice(7 - bytesLength);
          for (var st = 1; st < bytesLength; st++) {
            store += _arr[st + i].toString(2).slice(2);
          }
          str += String.fromCharCode(parseInt(store, 2));
          i += bytesLength - 1;
        } else {
          str += String.fromCharCode(_arr[i]);
        }
      }
      return str;
    }
  },
  mounted() {

  }
}
</script>

<style scoped>
img {
  width: 600px;
  height: 400px;
}
</style>
